package com.zhihuichongcheng.application.ui.task;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.base.BaseActivity;

/**
 * edit activity Created by LK on 2017/1/9.
 */
public class ContentEditActivity extends BaseActivity {
    public static final String EXTRA_HINT = "extra_hint";
    public static final String EXTRA_CONTENT_DEFAULT = "extra_content_default";
    public static final String EXTRA_RESULT = "extra_result";
    public static final String EXTRA_EDITABLE = "extra_editable";

    private String strHint = "请输入内容";
    private String strDefaultContent = "";
    private boolean enable = true;

    private EditText mInput;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content_edit);
        if (null != getIntent().getExtras()) {
            if (getIntent().getExtras().containsKey(EXTRA_HINT)) {
                strHint = getIntent().getExtras().getString(EXTRA_HINT);
            }
            if (getIntent().getExtras().containsKey(EXTRA_CONTENT_DEFAULT)) {
                strDefaultContent = getIntent().getExtras().getString(EXTRA_CONTENT_DEFAULT);
            }
            if (getIntent().getExtras().containsKey(EXTRA_EDITABLE)) {
                enable = getIntent().getExtras().getBoolean(EXTRA_EDITABLE);
            }
        }
        initView();
    }

    private void initView() {
        getTvArrowTitle().setVisibility(View.INVISIBLE);
        if (enable) {
            setTvRight("保存", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    save();
                }
            });
        }

        mInput = (EditText) this.findViewById(R.id.ed_content);
        mInput.setHint(strHint);
        mInput.setText(strDefaultContent);
        mInput.setEnabled(enable);
    }

    private void save() {
        Intent intent = new Intent();
        intent.putExtra(EXTRA_RESULT, mInput.getText().toString().trim());
        setResult(EXTRA_RESPONSE_CODE_SUCCESS, intent);
        finish();
    }
}
